<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="周次" prop="week1">
        <el-col class="line" :span="4">
          <el-form-item prop="week1">
            <el-input
              v-model="dataForm.weekStart"
              placeholder="起始周"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col class="inline" :span="2"> &nbsp;&nbsp; </el-col>
        <el-col class="line" :span="4">
          <el-form-item prop="week2">
            <el-input
              v-model="dataForm.weekEnd"
              placeholder="结束周"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="周几" prop="dayOfWeek">
        <el-checkbox-group v-model="dataForm.dayOfWeek">
          <el-checkbox
            v-for="weekDay in weekDays"
            :label="weekDay"
            :key="weekDay"
            >{{ weekDay }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="第几节课" prop="timeOfDay">
        <el-checkbox-group v-model="dataForm.timeOfDay">
          <el-checkbox v-for="time in timesOfDay" :label="time" :key="time">{{
            time
          }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="课程id" prop="courseId">
        <el-select
          ref="groupSelect"
          v-model="dataForm.courseId"
          placeholder="请选择时间后选择课程"
          @change="courseChange"
        >
          <el-option
            v-for="course in courses"
            :key="course.id"
            :label="course.courseName"
            :value="course.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="实验室" prop="labId">
        <el-col class="inline" :span="8">
          <el-select
            v-model="dataForm.labId"
            filterable
            placeholder="可供选择的实验室"
          >
            <el-option
              v-for="item in labOptions"
              :key="item.labId"
              :label="item.labName"
              :value="item.labId"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col class="inline" :span="5">
          <el-button round @click="courseChange">刷新实验室</el-button>
        </el-col>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      labOptions: [],
      weekDays: [1, 2, 3, 4, 5, 6, 7],
      timesOfDay: [1, 3, 5, 7, 9],
      courses: [],
      visible: false,
      dataForm: {
        id: 0,
        labId: "",
        courseId: "",
        weekStart: '',
        weekEnd: '',
        dayOfWeek: [],
        timeOfDay: [],
      },
      dataRule: {
        // week1: [
        //   { required:true, message: "起始周不能为空", trigger: "blur" }
        // ],
        // week2: [
        //   { required:true, message: "结束周不能为空", trigger: "blur" }
        // ],
        // labId: [  
        //   { required: true, message: "实验室id不能为空", trigger: "blur" },
        // ],
        // courseId: [
        //   { required: true, message: "课程id不能为空", trigger: "blur" },
        // ],
        // dayOfWeek: [
        //   { min:1, message: "周几不能为空", trigger: "blur" },
        // ],
        // timeOfDay: [
        //   { min:1, message: "第几节课不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  created() {
    this.getLabNameList();
    this.loadAllCourses();
  },
  watch: {
    dataForm: "loadAllCourses",
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/lab/reservation/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm.labId = data.reservation.labId;
              this.dataForm.courseId = data.reservation.courseId;
              this.dataForm.week = data.reservation.week;
              this.dataForm.dayOfWeek = data.reservation.dayOfWeek;
              this.dataForm.timeOfDay = data.reservation.timeOfDay;
            }
          });
        }
      });
    },
    courseChange() {
      console.log("courseChange!!!");
      this.getLabNameList();
      console.log("getLabNameList!!");
    },
    loadAllCourses() {
      this.$http({
        url: this.$http.adornUrl("/lab/course/listByTID"),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 200) {
          this.courses = data.page.list;
          console.log(this.courses);
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    handleSelect(item) {
      console.log(item);
    },
    getLabNameList() {
      this.$http({
        url: this.$http.adornUrl("/lab/reservation/listLabNameByTimeAndCourse"),
        method: "get",
        params: this.$http.adornParams({
          courseId: this.dataForm.courseId,
          weekStart: this.dataForm.weekStart,
          weekEnd: this.dataForm.weekEnd,
          dayOfWeek: this.dataForm.dayOfWeek.toString(),
          timeOfDay: this.dataForm.timeOfDay.toString(),
        }),
      }).then(({ data }) => {
        if (data && data.code === 200) {
          this.labOptions = data.page.list;
          console.log(this.labOptions);
        } else {
          this.labOptions = [];
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/lab/reservation/`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              labId: this.dataForm.labId,
              courseId: this.dataForm.courseId,
              week: this.dataForm.week,
              dayOfWeek: this.dataForm.dayOfWeek,
              timeOfDay: this.dataForm.timeOfDay,
            }),
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
